import { Card, Form, Input, Button, Checkbox } from 'antd'
import logo from '../../assets/logo.png'
import styles from './index.module.scss'
import { useDispatch } from 'react-redux'
import { login } from '@/store/actions/login'

const Login = () => {
  const dispatch = useDispatch()
  const onFinish = (values) => {
    // values 能自动收集表单的值
    console.log('Success:', values)
    // 发送请求,进行登录
    dispatch(login(values))
  }

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo)
  }
  return (
    <div className={styles.root}>
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 登录表单 */}
        <Form
          validateTrigger={['onBlur']}
          name="basic"
          initialValues={{ code: '246810', mobile: '13828982051', isAgree: true }} // 初始值
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off">
          <Form.Item
            name="mobile"
            rules={[
              {
                pattern: /^1[3-9]\d{9}$/,
                message: '手机号码格式不对'
              },
              { required: true, message: '请输入手机号' }
            ]}>
            <Input placeholder="请输入手机号" />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              { pattern: /^\d{6}$/, message: '请输入合法的验证码！' },
              { required: true, message: '请输入验证码' }
            ]}>
            <Input placeholder="请输入验证码" />
          </Form.Item>

          <Form.Item
            name="isAgree"
            valuePropName="checked"
            rules={[
              {
                validator (rule, value) {
                  if (value) {
                    return Promise.resolve()
                  } else {
                    return Promise.reject(new Error('必须同意'))
                  }
                }
              }
            ]}>
            <Checkbox>我已阅读并同意[隐私条款]和[用户协议]</Checkbox>
          </Form.Item>

          <Form.Item>
            <Button type="primary" block htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Login
